package com.you.controller;

import com.you.common.Result;
import com.you.entity.Menu;
import com.you.entity.User;
import com.you.service.impl.MenuServiceImpl;
import com.you.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

@RestController
public class LoginController {

    @Autowired
    UserServiceImpl userService;
    @Autowired
    MenuServiceImpl menuService;

    //登录
    @PostMapping("/login")
    public Result login(@RequestBody User user){

        List<User> userList = userService.lambdaQuery().eq(User::getNo, user.getNo())
                                                    .eq(User::getPassword, user.getPassword())
                                                    .list();

        if (userList.size()>0){
           User user1 =  userList.get(0);
            List<Menu> menuList = menuService.lambdaQuery().like(Menu::getMenuRight, user1.getRoleId()).list();

            HashMap map = new HashMap<>();
            map.put("user",user1);
            map.put("menu",menuList);

            return Result.suc(map);

        }else {
            return Result.fail();
        }

    }
}
